package org.jeecg.modules.config;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.entity.RunnerMatchPlayer;
import org.jeecg.modules.entity.RunnerMatchScore;
import org.jeecg.modules.jmreport.api.data.IDataSetFactory;
import org.jeecg.modules.jmreport.desreport.model.JmPage;
import org.jeecg.modules.service.IRunnerMatchPlayerService;
import org.jeecg.modules.service.IRunnerMatchScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author tanxy
 * @date 2022年12月13日 23:57
 */
@Component
public class MatchScoreRpSpringBean implements IDataSetFactory {
    @Override
    public List<Map<String, Object>> createData(Map<String, Object> map) {
        IRunnerMatchPlayerService playerService = SpringContextUtils.getBean(IRunnerMatchPlayerService.class);
        IRunnerMatchScoreService scoreService = SpringContextUtils.getBean(IRunnerMatchScoreService.class);
        String matchId = (String)map.get("matchId");
        String groupId = (String)map.get("groupid");
        List<String> query = new ArrayList<>();
        if(StrUtil.isNotEmpty(groupId)){
            query.add(groupId);
        }
        List<RunnerMatchPlayer> matchPlayers = playerService.selectByMainIdAndGroupId(matchId,query);
        List<Map<String,Object>> res = new ArrayList<>();
        for(RunnerMatchPlayer player:matchPlayers){
            Map<String,Object> resMap = new HashMap<>();
            QueryWrapper<RunnerMatchScore> wrapper = new QueryWrapper<>();
            wrapper.eq("matchid",matchId).eq("player",player.getPlayer());
            List<RunnerMatchScore> runnerMatchScores = scoreService.list(wrapper);
            resMap.put("play",player.getPlayer());
            resMap.put("playcode",player.getPcode());
            resMap.put("groupid",player.getGroupid());
            resMap.put("pstarttime",player.getPstarttime());
            resMap.put("score",player.getScore());
            resMap.put("scorecj",player.getScorecj());
            if(player.getPendtime()==null){
                resMap.put("updateTime",new Date());
            }else{
                resMap.put("updateTime",player.getPendtime());
            }
            if(runnerMatchScores.size()==0){
                resMap.put("updateTime",player.getUpdateTime());
            }else {
                for(RunnerMatchScore score:runnerMatchScores){
                    resMap.put(score.getTimePoint(),score.getCleantime());
                }
            }
            res.add(resMap);
        }
        List<Map<String, Object>> realList = new ArrayList<>();
        for (Map<String, Object> tmp:res){
            Date updateTime = (Date)tmp.get("updateTime");
            //yyyy-MM-dd HH:mm:ss
            Date pstarttime = (Date)tmp.get("pstarttime");
            if(ObjectUtil.isNotEmpty(updateTime)){
                String s = DateUtils.formatDate(pstarttime,"yyyy-MM-dd HH:mm:ss");
                tmp.put("starttime",s);
                realList.add(tmp);
            }
        }

        Collections.sort(realList, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Date updateTime1 = (Date)o1.get("updateTime");
                Date updateTime2 = (Date)o2.get("updateTime");
                return updateTime1.compareTo(updateTime2);
            }
        });

       /* Map<String,Object> resMap = new HashMap<>();
        resMap.put("play","");
        resMap.put("groupid","");
        resMap.put("play","");
        resMap.put("pstarttime","");
        resMap.put("score","");
        resMap.put("scorecj","");
        resMap.put("5km","");
        resMap.put("15km","");
        resMap.put("30km","");
        res.add(resMap);*/
        return realList;
    }

    @Override
    public JmPage createPageData(Map<String, Object> map) {
        return null;
    }
}
